<template>
    <div>
        <div class="operate center-flex-box" v-if="hasPermission('PAGE_USER_ROLE')">
            <el-button class="item" type="primary" v-if="hasPermission('ADD_USER_ROLE')" @click="showEditor(null,null)">
                新增
            </el-button>
            <el-input placeholder="请输入内容" v-model="queryInfo.content" class="input-with-select item">
                <el-select v-model="queryInfo.mode" slot="prepend" placeholder="请选择">
                    <el-option label="无" value="none"></el-option>
                    <el-option label="用户ID" value="userIds"></el-option>
                    <el-option label="角色ID" value="roleIds"></el-option>
                    <el-option label="操作区域" value="operateAreas"></el-option>
                </el-select>
            </el-input>
            <div class="item center-flex-box">
                <el-checkbox v-model="queryInfo.checked">包含本区域</el-checkbox>
                <el-checkbox v-model="queryInfo.withSelf" :disabled="!queryInfo.checked">包含</el-checkbox>
            </div>
            <el-select v-model="queryInfo.statuses" multiple placeholder="状态">
                <el-option label="不可用" value="-1"></el-option>
                <el-option label="正常" value="0"></el-option>
                <el-option label="锁定" value="1"></el-option>
            </el-select>
            <el-cascader
                class="item"
                placeholder="排序"
                expand-trigger="hover"
                :options="options"
                v-model="queryInfo.sort">
            </el-cascader>
            <el-button class="item" @click="obtainUserRoles">查找</el-button>
        </div>
        <el-table
            v-if="hasPermission('PAGE_USER_ROLE')"
            :data="userRoles"
            stripe
            max-height="532"
            style="width: 100%">
            <el-table-column
                prop="userId"
                label="用户ID"
                sortable
                width="180">
            </el-table-column>
            <el-table-column
                prop="role.id"
                label="角色ID"
                sortable
                width="180">
            </el-table-column>
            <el-table-column
                prop="role.name"
                label="角色名称"
                sortable
                width="180">
            </el-table-column>
            <el-table-column
                prop="role.description"
                label="角色描述"
                width="200">
            </el-table-column>
            <el-table-column
                label="角色状态"
                width="120">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.role.enable" type="success">启用</el-tag>
                    <el-tag v-else type="danger">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                prop="operateArea"
                label="操作区域"
                sortable
                width="120">
            </el-table-column>
            <el-table-column
                prop="withSelf"
                label="包含本区域"
                sortable
                width="120">
            </el-table-column>
            <el-table-column
                prop="level"
                label="等级"
                sortable
                width="120">
            </el-table-column>
            <el-table-column
                prop="status"
                label="状态"
                sortable
                width="120">
            </el-table-column>
            <el-table-column
                prop="recoveryTime"
                label="恢复时间"
                sortable
                width="160">
            </el-table-column>
            <el-table-column
                prop="description"
                label="描述"
                width="200">
            </el-table-column>
            <el-table-column
                v-if="hasPermission('UPDATE_USER_ROLE')||hasPermission('DELETE_USER_ROLE')"
                fixed="right"
                width="180"
                label="操作">
                <template slot-scope="scope">
                    <el-button @click="showEditor(scope.row.userId,scope.row.role.id)"
                               v-if="hasPermission('UPDATE_USER_ROLE')"
                               type="primary"
                               size="mini"
                               plain>
                        编辑
                    </el-button>
                    <el-button @click="remove(scope.row.userId,scope.row.role.id)"
                               v-if="hasPermission('DELETE_USER_ROLE')"
                               type="danger"
                               size="mini"
                               plain>
                        删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-dialog :title="operate==='insert'?'新增用户角色':'修改用户角色'" :visible.sync="editor" width="40%">
            <el-form ref="userRole" :rules="rules" :model="userRole" label-position="left" label-width="120px"
                     size="small">
                <el-form-item label="用户" prop="userId">
                    <el-select class="left" v-model="userRole.userId" placeholder="请选择用户"
                               :disabled="operate==='update'">
                        <el-option
                            v-for="user in users"
                            :key="user.id"
                            :label="user.account"
                            :value="user.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="角色" prop="roleId">
                    <el-select class="left" v-model="userRole.roleId" placeholder="请选择角色"
                               :disabled="operate==='update'">
                        <el-option
                            v-for="role in roles"
                            :key="role.id"
                            :label="role.name"
                            :value="role.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="操作区域" prop="operateArea">
                    <el-select class="left" v-model="userRole.operateArea" placeholder="请选择管理区域">
                        <el-option label="-" value="-"></el-option>
                        <el-option
                            v-for="area in areas"
                            :key="area.id"
                            :label="area.name"
                            :value="area.name">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否包含本区域">
                    <div class="withSelf">
                        <el-switch v-model="userRole.withSelf"></el-switch>
                    </div>
                </el-form-item>
                <el-form-item label="等级">
                    <el-slider
                        v-model="userRole.level"
                        :min="-1"
                        :max="99"
                        show-input>
                    </el-slider>
                </el-form-item>
                <el-form-item label="状态" prop="status">
                    <el-select class="left" v-model="userRole.status" placeholder="请选择状态">
                        <el-option label="不可用" value="-1"></el-option>
                        <el-option label="正常" value="0"></el-option>
                        <el-option label="锁定" value="1"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="恢复时间" v-if="userRole.status==='1'" prop="recoveryTime">
                    <el-date-picker
                        class="left"
                        v-model="userRole.recoveryTime"
                        type="datetime"
                        placeholder="选择日期时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="描述">
                    <el-input type="textarea" v-model="userRole.description"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editor = false">取 消</el-button>
                <el-button type="primary" @click="commit">提交</el-button>
            </div>
        </el-dialog>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="pageInfo.page"
            :page-size="pageInfo.size"
            layout="total, prev, pager, next, jumper"
            :total="pageInfo.total">
        </el-pagination>
    </div>
</template>

<script>
    import { mapGetters, mapActions } from 'vuex'
    import { convertTime } from '@/utils/date'

    export default {
        name: 'userRoleManagement',
        data () {
            return {
                userRoles: [],
                userRole: { userId: '', roleId: '', level: -1, withSelf: true },
                pageInfo: {
                    page: 1,
                    size: 100,
                    total: 0
                },
                queryInfo: {
                    mode: '',
                    content: '',
                    operateAreas: '',
                    sort: [],
                    statuses: [],
                    checked: false,
                    withSelf: false
                },
                editor: false,
                operate: 'insert',
                users: [],
                roles: [],
                areas: [],
                rules: {
                    userId: [{ required: true, message: '请选择用户', trigger: 'blur' }],
                    roleId: [{ required: true, message: '请选择角色', trigger: 'blur' }],
                    operateArea: [{ required: true, message: '请选择操作区域', trigger: 'blur' }],
                    status: [{ required: true, message: '请选择状态', trigger: 'blur' }],
                    recoveryTime: [{ type: 'date', required: true, message: '请选择恢复时间', trigger: 'blur' }]
                },
                options: [{
                    label: '无',
                    value: 'none'
                }, {
                    label: '用户ID',
                    value: 'user_id',
                    children: [{
                        label: '正序',
                        value: 'asc'
                    }, {
                        label: '倒序',
                        value: 'desc'
                    }]
                }, {
                    label: '角色ID',
                    value: 'role_id',
                    children: [{
                        label: '正序',
                        value: 'asc'
                    }, {
                        label: '倒序',
                        value: 'desc'
                    }]
                }, {
                    label: '操作区域',
                    value: 'operate_area',
                    children: [{
                        label: '正序',
                        value: 'asc'
                    }, {
                        label: '倒序',
                        value: 'desc'
                    }]
                }, {
                    label: '等级',
                    value: 'level',
                    children: [{
                        label: '正序',
                        value: 'asc'
                    }, {
                        label: '倒序',
                        value: 'desc'
                    }]
                }, {
                    label: '状态',
                    value: 'status',
                    children: [{
                        label: '正序',
                        value: 'asc'
                    }, {
                        label: '倒序',
                        value: 'desc'
                    }]
                }, {
                    label: '恢复时间',
                    value: 'recovery_time',
                    children: [{
                        label: '正序',
                        value: 'asc'
                    }, {
                        label: '倒序',
                        value: 'desc'
                    }]
                }]
            }
        },
        mounted () {
            this.obtainUserRoles()
            this.action(this.listUserKeyInfoAction(), data => {
                this.users = data.data
            })
            this.action(this.listRoleInfoAction(), data => {
                this.roles = data.data
            })
            this.action(this.listAreaAction(), data => {
                this.areas = data.data
            })
        },
        computed: {
            ...mapGetters({
                hasPermission: 'user/hasPermission'
            })
        },
        methods: {
            ...mapActions({
                listAreaAction: 'area/listArea',
                listUserKeyInfoAction: 'user/listUserKeyInfo',
                listRoleInfoAction: 'user/listRoleInfo',
                pageUserRoleAction: 'user/pageUserRole',
                insertUserRoleAction: 'user/insertUserRole',
                updateUserRoleAction: 'user/updateUserRole',
                deleteUserRoleAction: 'user/deleteUserRole'
            }),
            handleSizeChange (val) {
                this.pageInfo.size = val
                this.obtainUserRoles()
            },
            handleCurrentChange (val) {
                this.pageInfo.page = val
                this.obtainUserRoles()
            },
            obtainUserRoles () {
                this.action(this.pageUserRoleAction(this.obtainQuery()), data => {
                    if (data.data) {
                        this.userRoles = data.data.records
                        this.userRoles.forEach(userRole => {
                            userRole.withSelf = userRole.withSelf + ''
                            userRole.recoveryTime = userRole.recoveryTime ? convertTime(userRole.recoveryTime) : ''
                            switch (userRole.status) {
                                case -1:
                                    userRole.status = '不可用'
                                    break
                                case 0:
                                    userRole.status = '正常'
                                    break
                                case 1:
                                    userRole.status = '锁定'
                                    break
                            }
                        })
                        this.pageInfo.total = data.data.total
                    } else {
                        this.userRoles = []
                    }
                })
            },
            obtainQuery () {
                let query = {
                    page: this.pageInfo.page,
                    size: this.pageInfo.size,
                    operateAreas: this.queryInfo.operateAreas,
                    statuses: this.queryInfo.statuses,
                    withSelf: this.queryInfo.checked ? this.queryInfo.withSelf : null
                }
                if (this.queryInfo.sort.length === 2) {
                    query[this.queryInfo.sort[1]] = this.queryInfo.sort[0]
                }
                query[this.queryInfo.mode] = this.queryInfo.content
                return query
            },
            showEditor (userId, roleId) {
                if (userId && roleId) {
                    this.userRole = { ...this.userRoles.find(userRole => userRole.userId === userId && userRole.role.id === roleId) }
                    this.userRole.roleId = this.userRole.role.id
                    delete this.userRole.role
                    switch (this.userRole.status) {
                        case '不可用':
                            this.userRole.status = '-1'
                            break
                        case '正常':
                            this.userRole.status = '0'
                            break
                        case '锁定':
                            this.userRole.status = '1'
                            break
                    }
                    this.operate = 'update'
                } else {
                    this.userRole = { userId: '', roleId: '', level: -1, withSelf: true }
                    this.operate = 'insert'
                }
                this.editor = true
            },
            commit () {
                this.$refs['userRole'].validate((valid) => {
                    if (valid) {
                        if (this.operate === 'insert') {
                            this.action(this.insertUserRoleAction(this.userRole), data => {
                                this.$message.success('添加成功')
                                this.editor = false
                                this.obtainUserRoles()
                            })
                        } else if (this.operate === 'update') {
                            this.action(this.updateUserRoleAction(this.userRole), data => {
                                this.$message.success('更新成功')
                                this.editor = false
                                this.obtainUserRoles()
                            })
                        }
                    } else {
                        return false
                    }
                })
            },
            remove (userId, roleId) {
                this.action(this.deleteUserRoleAction({ userId, roleId }), data => {
                    this.$message.success('删除成功')
                    this.obtainUserRoles()
                })
            }
        }
    }
</script>

<style lang="scss" scoped>
    @import '../../../../../style/scss';

    .operate {
        height: 80px;
        width: 96%;
        padding: 0 2%;

        .item {
            margin: 6px;
        }

        .input-with-select {
            width: 360px;
            background-color: #fff;

            .el-select {
                width: 100px;
            }
        }
    }

    .withSelf {
        width: 15%;
    }

    .left {
        float: left;
    }
</style>
